package com.edu.bean;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

/**
 * @Author:何包蛋
 * @Date :2022/11/1 - 11 - 01 - 11:08
 */
public class MyIOC {

    private Map<String,Object> map = new HashMap<String, Object>();

    public MyIOC(String path){
        Properties pro = new Properties();
        InputStream inputStream = MyIOC.class.getClassLoader().getResourceAsStream(path);
        try {
            pro.load(inputStream);
            Set<String> keys = pro.stringPropertyNames();
            for (String key : keys) {
                String className = pro.getProperty(key);
                Class clazz = Class.forName(className);
                Object value = clazz.newInstance();
                map.put(key,value);
            }
            
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
    }

    public Object getBean(String key) {
        return map.get(key);
    }
}
